package com.zj.web.controller;

import com.zj.service.BannerService;
import com.zj.service.CategoryService;
import com.zj.web.bean.BannerDTO;
import com.zj.web.bean.CategoryDTO;
import com.zj.web.bean.ResponseResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/categorys")
@RefreshScope //开启动态刷新
@Tag(name = "分类控制器",description = "提供对分类资源的添加，查询并显示等操作")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

//    @Value("${product.pattern.dateFormat}")
//    private String dateFormat;

    @PostMapping("/add")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ResponseResult> add(CategoryDTO categoryDTO){
        CategoryDTO category =categoryService.addCategory(categoryDTO);
        return ResponseEntity.ok(ResponseResult.ok().setdata(category));
    }

    /**
     * 前台：根据分类id修改分类状态
     * */
    @PutMapping("/updateCategoryStatus/{categoryId}/{categoryStatus}")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ResponseResult> updateCategoryStatus(@PathVariable("categoryId") Long categoryId,@PathVariable("categoryStatus") Integer categoryStatus){
        CategoryDTO categoryDTO = categoryService.updateCategoryStatus(categoryId, categoryStatus);
        return ResponseEntity.ok(ResponseResult.ok().setdata(categoryDTO));
    }
    /**
     * 前台：根据分类id更新分类信息
     * */
    @PreAuthorize("hasRole('ADMIN')")
    @PutMapping("/updateCategory/{categoryId}")
    public ResponseEntity<ResponseResult> updateCategory(@PathVariable("categoryId") Long categoryId,
                                                         @RequestParam("categoryName") String categoryName,
                                                         @RequestParam("linkUrl") String linkUrl,
                                                         @RequestPart("icoImgFile") MultipartFile icoImgFile){
        CategoryDTO categoryDTO = new CategoryDTO();
        categoryDTO.setCategoryId(categoryId);
        categoryDTO.setCategoryName(categoryName);
        categoryDTO.setLinkUrl(linkUrl);
        categoryDTO.setIcoImgFile(icoImgFile);
        CategoryDTO categoryDTO1 = categoryService.updateCategory(categoryDTO);
        return ResponseEntity.ok(ResponseResult.ok().setdata(categoryDTO1));
    }
}